package cn.com.chec.en.controller.synch;

import cn.com.chec.en.dao.UserInfoDao;
import cn.com.chec.en.model.domain.UserInfo;
import cn.com.chec.en.model.domain.WebsiteInfo;
import cn.com.chec.en.util.BlowFish;
import cn.com.chec.en.util.WebUtils;
import com.google.gson.Gson;
import java.io.PrintWriter;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class UserInfoSynchController {
	protected static final Logger logger = Logger
			.getLogger(UserInfoSynchController.class);
	private static final BlowFish bf = new BlowFish(
			"34b643c6b5144ec594b0df734d311399");
	private static final Gson gson = new Gson();

	@Resource
	private UserInfoDao userInfoDao;

	@RequestMapping(value = { "/synch/userInfo/getAllAdmin.do" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	public void getAllAdmin(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		PrintWriter out = response.getWriter();
		String sign = WebUtils.getRequestParameterAsString(request, "sign");
		if ((StringUtils.isBlank(sign))
				|| (!StringUtils.equals("c05f2c0f21f749519a55e621c1a16d77",
						bf.decryptString(sign)))) {
			out.print("-1");
			return;
		}
		List<UserInfo> userInfoList = this.userInfoDao.getUserListByRoleId(1, 0,
				2147483647);
		String result = gson.toJson(userInfoList, List.class);
		out.print("1,");
		out.print(bf.encryptString(result));
	}

	@RequestMapping(value = { "/synch/userInfo/addAdmin.do" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	public void addAdmin(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		PrintWriter out = response.getWriter();
		String sign = WebUtils.getRequestParameterAsString(request, "sign");
		if ((StringUtils.isBlank(sign))
				|| (!StringUtils.equals("c05f2c0f21f749519a55e621c1a16d77",
						bf.decryptString(sign)))) {
			out.print("-1");
			return;
		}

		String userInfo = WebUtils.getRequestParameterAsString(request,
				"userInfo");
		String websiteInfo = WebUtils.getRequestParameterAsString(request,
				"websiteInfo");
		if ((StringUtils.isBlank(userInfo))
				|| (StringUtils.isBlank(websiteInfo))) {
			out.print("0");
			return;
		}
		UserInfo user = (UserInfo) gson.fromJson(userInfo, UserInfo.class);
		WebsiteInfo website = (WebsiteInfo) gson.fromJson(websiteInfo,
				WebsiteInfo.class);
		String message = "";
		if (this.userInfoDao.createUser(user)) {
			out.print("1,");
			message = "创建<font color='red'>" + website.getName()
					+ "</font>用户成功!";
		} else {
			out.print("0,");
			message = "创建<font color='red'>" + website.getName()
					+ "</font>用户失败!";
		}
		out.print(bf.encryptString(message));
	}

	@RequestMapping(value = { "/synch/userInfo/delAdmin.do" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	public void delAdmin(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		PrintWriter out = response.getWriter();
		String sign = WebUtils.getRequestParameterAsString(request, "sign");
		if ((StringUtils.isBlank(sign))
				|| (!StringUtils.equals("c05f2c0f21f749519a55e621c1a16d77",
						bf.decryptString(sign)))) {
			out.print("-1");
			return;
		}

		String userIdString = WebUtils.getRequestParameterAsString(request,
				"userId");
		String websiteInfo = WebUtils.getRequestParameterAsString(request,
				"websiteInfo");
		if ((StringUtils.isBlank(userIdString))
				|| (StringUtils.isBlank(websiteInfo))) {
			out.print("0");
			return;
		}
		int userId = Integer.valueOf(bf.decryptString(userIdString)).intValue();
		WebsiteInfo website = (WebsiteInfo) gson.fromJson(
				bf.decryptString(websiteInfo), WebsiteInfo.class);
		String message = "";
		if (this.userInfoDao.delUser(userId)) {
			out.print("1,");
			message = "删除<font color='red'>" + website.getName()
					+ "</font>用户成功!";
		} else {
			out.print("0,");
			message = "删除<font color='red'>" + website.getName()
					+ "</font>用户失败!";
		}
		out.print(bf.encryptString(message));
	}

	@RequestMapping(value = { "/synch/userInfo/detailAdmin.do" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	public void detailAdmin(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		PrintWriter out = response.getWriter();
		String sign = WebUtils.getRequestParameterAsString(request, "sign");
		if ((StringUtils.isBlank(sign))
				|| (!StringUtils.equals("c05f2c0f21f749519a55e621c1a16d77",
						bf.decryptString(sign)))) {
			out.print("-1");
			return;
		}

		String userIdString = WebUtils.getRequestParameterAsString(request,
				"userId");
		if (StringUtils.isBlank(userIdString)) {
			out.print("0");
			return;
		}
		int userId = Integer.valueOf(bf.decryptString(userIdString)).intValue();
		UserInfo userInfo = this.userInfoDao.getUser(userId);
		out.print("1,");
		out.print(bf.encryptString(gson.toJson(userInfo, UserInfo.class)));
	}

	@RequestMapping(value = { "/synch/userInfo/updateAdmin.do" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	public void updateAdmin(HttpServletRequest request,
			HttpServletResponse response, HttpSession session) throws Exception {
		String message = "";
		PrintWriter out = response.getWriter();
		String sign = WebUtils.getRequestParameterAsString(request, "sign");
		if ((StringUtils.isBlank(sign))
				|| (!StringUtils.equals("c05f2c0f21f749519a55e621c1a16d77",
						bf.decryptString(sign)))) {
			out.print("-1");
			return;
		}

		String userInfoString = WebUtils.getRequestParameterAsString(request,
				"userInfo");
		String websiteInfoString = WebUtils.getRequestParameterAsString(
				request, "websiteInfo");
		if ((StringUtils.isBlank(userInfoString))
				|| (StringUtils.isBlank(websiteInfoString))) {
			out.print("0");
			return;
		}
		UserInfo userInfo = (UserInfo) gson.fromJson(
				bf.decryptString(userInfoString), UserInfo.class);
		WebsiteInfo websiteInfo = (WebsiteInfo) gson.fromJson(
				bf.decryptString(websiteInfoString), WebsiteInfo.class);
		if (this.userInfoDao.updateUser(userInfo)) {
			out.print("1,");
			message = "修改<font color='red'>" + websiteInfo.getName()
					+ "</font>用户成功!";
		} else {
			out.print("0,");
			message = "修改<font color='red'>" + websiteInfo.getName()
					+ "</font>用户失败!";
		}
		out.print(bf.encryptString(message));
	}

	@RequestMapping(value = { "/synch/userInfo/restPwdAdmin.do" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	public void restPwdAdmin(HttpServletRequest request,
			HttpServletResponse response, HttpSession session) throws Exception {
		String message = "";
		PrintWriter out = response.getWriter();
		String sign = WebUtils.getRequestParameterAsString(request, "sign");
		if ((StringUtils.isBlank(sign))
				|| (!StringUtils.equals("c05f2c0f21f749519a55e621c1a16d77",
						bf.decryptString(sign)))) {
			out.print("-1");
			return;
		}

		String userIdString = WebUtils.getRequestParameterAsString(request,
				"userId");
		String websiteInfoString = WebUtils.getRequestParameterAsString(
				request, "websiteInfo");
		if ((StringUtils.isBlank(userIdString))
				|| (StringUtils.isBlank(websiteInfoString))) {
			out.print("0");
			return;
		}
		int userId = Integer.valueOf(bf.decryptString(userIdString)).intValue();
		WebsiteInfo websiteInfo = (WebsiteInfo) gson.fromJson(
				bf.decryptString(websiteInfoString), WebsiteInfo.class);
		if (this.userInfoDao.resetUserPwd("admin", userId)) {
			out.print("1,");
			message = "<font color='red'>" + websiteInfo.getName()
					+ "</font>重置密码成功,新密码为\"admin\",请妥善保管!";
		} else {
			out.print("0,");
			message = "<font color='red'>" + websiteInfo.getName()
					+ "</font>重置失败!";
		}
		out.print(bf.encryptString(message));
	}
}